package net.godbirds.market.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import net.godbirds.bean.Base;
import net.godbirds.bean.Code;
import net.godbirds.market.service.WelcomeService;
import net.godbirds.util.HttpUtils;
import net.godbirds.util.JSONUtils;
/**
 * 首页服务实现
 * @author Darker.Wang
 *
 */
@Service
public class WelcomeServiceImpl implements WelcomeService{

	@Value("${global.getway.url}")
	String getway;
	@Value("${global.server.url}")
	String server;
	
	@Autowired
	RestTemplate restTemplate;
	
	@Override
	public List<Code> getCodes(String type) {
		// TODO Auto-generated method stub
		String url = getway+server+"/code/queryList?type="+type;
		String codeStr = HttpUtils.doGet(url);
		JSONArray jsonArray = JSON.parseArray(codeStr);
		List<Code> codes = new ArrayList<Code>();
		for(int i = 0; i < jsonArray.size(); i++) {
			Code code = new Code();
			JSONObject job = (JSONObject) jsonArray.get(i);
			code.setNum(i);
			code.setCode((String)JSONUtils.getValue(job, "code"));
			code.setName((String)JSONUtils.getValue(job, "codeName"));
			code.setAlias((String)JSONUtils.getValue(job, "codeAlias"));
			codes.add(code);
		}
		return codes;
	}
	@Override
	public List<Code> getCodes(String type,String group) {
		// TODO Auto-generated method stub
		String url = getway+server+"/code/queryGroup?type="+type+"&group="+group;
		String codeStr = HttpUtils.doGet(url);
		JSONArray jsonArray = JSON.parseArray(codeStr);
		List<Code> codes = new ArrayList<Code>();
		for(int i = 0; i < jsonArray.size(); i++) {
			Code code = new Code();
			code.setNum(i);
			JSONObject job = (JSONObject) jsonArray.get(i);
			code.setCode((String)JSONUtils.getValue(job, "code"));
			code.setName((String)JSONUtils.getValue(job, "codeName"));
			code.setAlias((String)JSONUtils.getValue(job, "codeAlias"));
			codes.add(code);
		}
		return codes;
	}
	@Override
	public List<Base> getPrdBase() {
		List<Code> codes = getCodes("PRODUCT_GROUP");
		List<Base> bases = convert2Base(codes);
		for(Base base : bases) {
			List<Code> tcodes = getCodes("PRODUCT_TYPE",base.getCode());
			List<Base> tbases = convert2Base(tcodes);
			base.setChildren(tbases);
		}
		return bases;
	}
	/**
	 * Code 转换成Base
	 * @param codes
	 * @return
	 */
	private List<Base> convert2Base(List<Code> codes){
		List<Base> bases = new ArrayList<Base>();
		for(Code code : codes) {
			Base base = new Base();
			base.setAlias(code.getAlias());
			base.setCode(code.getCode());
			base.setName(code.getName());
			base.setGroup(code.getGroup());
			base.setNum(code.getNum());
			bases.add(base);
		}
		return bases;
	}

}
